package com.qf.resolver;

import com.qf.ex.MyException1;
import com.qf.ex.MyException2;
import com.qf.ex.MyException3;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

//       1 创建ModelAndView 对象
        ModelAndView modelAndView = new ModelAndView();
//        2指定错误跳转的页面
        String jump = "redirect:";
//        3 对异常信息进行判断
        if (e instanceof MyException1) {
            jump += "/error1.jsp";
        } else if (e instanceof MyException2) {
            jump += "/error2.jsp";
        } else if (e instanceof MyException3) {
            jump += "/error3.jsp";
        } else if (e instanceof ArithmeticException) {
            jump += "/error4.jsp";
        }

        modelAndView.setViewName(jump);

        return modelAndView;
    }
}
